import { Role } from '../../types';
import { RoleRepository } from '../../repositories/system/RoleRepository';
import { HttpError } from '../../exceptions/HttpError';
export class RoleService {
    private roleRepository: RoleRepository;

    constructor() {
        this.roleRepository = new RoleRepository();
    }




    // 获取所有角色
    async getAllRoles(): Promise<Role[]> {
        try {
            return await this.roleRepository.findAll();
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`获取角色列表失败: ${error.message}`);
        }
    }

    // 根据ID获取角色
    async getRoleById(id: string): Promise<Role | null> {
        try {
            return await this.roleRepository.findById(id);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`获取角色信息失败: ${error.message}`);
        }
    }

    // 创建角色
    async createRole(roleData: Partial<Role>): Promise<Role> {
        try {
            return await this.roleRepository.create(roleData);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`创建字典项失败3: ${error.message}`);
        }
    }

    // 更新角色
    async updateRole(id: string, roleData: Partial<Role>): Promise<Role | null> {
        try {
            return await this.roleRepository.update(id, roleData);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`更新角色失败: ${error.message}`);
        }
    }

    // 删除用户
    async deleteRole(id: string): Promise<boolean> {
        try {
            return await this.roleRepository.delete(id);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`删除角色失败: ${error.message}`);
        }
    }

    // 根据类型查找角色
    async getRoleByType(type: string): Promise<Role[] | null> {
        try {
            return await this.roleRepository.findByType(type);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`查找角色失败: ${error.message}`);
        }
    }

    // 获取角色数量
    async getRoleCount(): Promise<number> {
        try {
            return await this.roleRepository.count();
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`统计角色数量失败: ${error.message}`);
        }
    }

    // 分页查询角色
    async getRolesWithPagination(page: number = 1, limit: number = 10): Promise<{ list: Role[], total: number, pages: number }> {
        try {
            return await this.roleRepository.findWithPagination(page, limit);
        } catch (error: any) {
            if (error instanceof HttpError) throw error;
            throw HttpError.internalServerError(`分页查询字典项失败: ${error.message}`);
        }
    }
}